package com.priox.sigar.application.job.mappers;

import com.priox.sigar.client.dto.BalanceComprobacionDTO;
import com.priox.sigar.client.dto.DocumentoAcreedorDTO;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: Matrix
 * Date: 10/02/13
 */

public class DocumentoAcreedorFieldSetMapper implements FieldSetMapper<DocumentoAcreedorDTO> {


    private static final Logger ERRORLOG = LoggerFactory.getLogger("errorLog");
    private static final Logger LOG = LoggerFactory.getLogger(DocumentoAcreedorFieldSetMapper.class);

    private Long checkPointId;
    private Long usuarioId = 1l;
    private Date fechaCorte;


    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet
     * (org.springframework.batch.item.file.transform.FieldSet)
     */
    public static void main(String[] args) {
        String patternNumeros = "[+-]*[0-9,0-9]*.[0-9]*";
        System.out.println("11".matches(patternNumeros));
        System.out.println("11.00".matches(patternNumeros));
        System.out.println("AAAgg8787".matches(patternNumeros));
        System.out.println("15,545,4".matches(patternNumeros));
        System.out.println("15,545,4.22".matches(patternNumeros));
        System.out.println("4.22".matches(patternNumeros));
        System.out.println("-4.22".matches(patternNumeros));
    }

    public DocumentoAcreedorDTO mapFieldSet(FieldSet fieldSet) throws BindException {
        DocumentoAcreedorDTO result = new DocumentoAcreedorDTO();
        String patternFecha = "^\\d{2}/\\d{2}/\\d{4}$";

        try {

            result.setUsuarioId(usuarioId);
            if (StringUtils.isNotBlank(fieldSet.readString(0))) {
                result.setNumeroDocumento(fieldSet.readString(0));
            }


            String acreedorId = fieldSet.readRawString(1);
           // boolean valido = false;
            if (StringUtils.isNotBlank(acreedorId)) {
               // valido = acreedorId.length() == 6 || acreedorId.length() == 8 || acreedorId.length() == 10;
              //  if (valido && NumberUtils.isNumber(acreedorId))
                    result.setAcreedorId(acreedorId);
            }

            String value = fieldSet.readString(2);
            if (StringUtils.isNotBlank(value) && value.matches(patternFecha)) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                result.setFechaDocumento(sdf.parse(value));
            }

            if (StringUtils.isNotBlank(fieldSet.readString(3)) && NumberUtils.isNumber(fieldSet.readString(3))) {
                result.setSociedadId(fieldSet.readString(3));
            }

            if (StringUtils.isNotBlank(fieldSet.readString(4))) {
                result.setClaseDocumento(fieldSet.readString(4));
            }
            if (StringUtils.isNotBlank(fieldSet.readString(5))) {
                result.setAcreedorNombre(fieldSet.readString(5));
            }
            if (StringUtils.isNotBlank(fieldSet.readString(6))) {
                result.setUsuario(fieldSet.readString(6));
            }


        } catch (Exception ex) {
            ERRORLOG.debug("error al leer el documento acreedor ", ex);
        }
        return result;
    }

    public Long getCheckPointId() {
        return checkPointId;
    }

    public void setCheckPointId(Long checkPointId) {
        this.checkPointId = checkPointId;
    }

    public Long getUsuarioId() {
        return usuarioId;
    }

    public void setUsuarioId(Long usuarioId) {
        this.usuarioId = usuarioId;
    }

    public Date getFechaCorte() {
        return fechaCorte;
    }

    public void setFechaCorte(Date fechaCorte) {
        this.fechaCorte = fechaCorte;
    }
}


